#!/bin/bash

# 支持多种模型服务的启动脚本

echo "🚀 基于Qwen2.5-VL的视频分析系统 - 多服务支持版本"
echo "=================================================="

# 显示帮助信息
show_help() {
    echo "使用方法: $0 [服务类型]"
    echo ""
    echo "支持的服务类型:"
    echo "  vllm     - 本地vLLM服务 (默认)"
    echo "  ollama   - Ollama本地服务"
    echo "  openai   - OpenAI API"
    echo "  azure    - Azure OpenAI"
    echo "  alibaba  - 阿里云通义千问"
    echo ""
    echo "示例:"
    echo "  $0 vllm      # 使用本地vLLM服务"
    echo "  $0 ollama    # 使用Ollama服务"
    echo "  $0 openai    # 使用OpenAI API"
    echo ""
    echo "环境变量配置:"
    echo "  复制 .env.example 为 .env 并修改相应配置"
}

# 检查参数
SERVICE_TYPE=${1:-vllm}

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    show_help
    exit 0
fi

# 验证服务类型
case $SERVICE_TYPE in
    vllm|ollama|openai|azure|alibaba)
        echo "✅ 选择的服务类型: $SERVICE_TYPE"
        ;;
    *)
        echo "❌ 不支持的服务类型: $SERVICE_TYPE"
        show_help
        exit 1
        ;;
esac

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "❌ Python3 未安装，请先安装Python 3.11+"
    exit 1
fi

# 创建虚拟环境（如果不存在）
if [ ! -d "venv" ]; then
    echo "📦 创建Python虚拟环境..."
    python3 -m venv venv
fi

# 激活虚拟环境
echo "🔧 激活虚拟环境..."
source venv/bin/activate

# 安装依赖
echo "📥 安装Python依赖..."
pip install -r requirements.txt

# 设置环境变量
export MODEL_CLIENT_TYPE=$SERVICE_TYPE

# 根据服务类型进行特定配置
case $SERVICE_TYPE in
    vllm)
        echo "🤖 配置vLLM服务..."
        export MODEL_BASE_URL="http://localhost:8001/v1"
        export MODEL_NAME="Qwen/Qwen2.5-VL-7B-Instruct"
        export MODEL_API_KEY="test"
        
        # 检查vLLM服务
        if curl -s http://localhost:8001/health > /dev/null 2>&1; then
            echo "✅ vLLM服务已运行"
        else
            echo "⚠️  vLLM服务未运行，请先启动:"
            echo "   vllm serve Qwen/Qwen2.5-VL-7B-Instruct --limit-mm-per-prompt image=4 --port 8001"
            echo ""
            read -p "是否自动启动vLLM服务? (y/N): " -n 1 -r
            echo
            if [[ $REPLY =~ ^[Yy]$ ]]; then
                echo "🚀 启动vLLM服务..."
                nohup vllm serve Qwen/Qwen2.5-VL-7B-Instruct --limit-mm-per-prompt image=4 --port 8001 > vllm.log 2>&1 &
                echo "⏳ 等待vLLM服务启动..."
                sleep 30
            fi
        fi
        ;;
        
    ollama)
        echo "🦙 配置Ollama服务..."
        export MODEL_BASE_URL="http://localhost:11434"
        export MODEL_NAME="qwen2-vl"
        export MODEL_API_KEY=""
        
        # 检查Ollama服务
        if curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
            echo "✅ Ollama服务已运行"
            
            # 检查模型是否已安装
            if ollama list | grep -q "qwen2-vl"; then
                echo "✅ qwen2-vl模型已安装"
            else
                echo "📥 安装qwen2-vl模型..."
                ollama pull qwen2-vl
            fi
        else
            echo "❌ Ollama服务未运行，请先启动Ollama"
            echo "   访问 https://ollama.ai 下载并安装Ollama"
            exit 1
        fi
        ;;
        
    openai)
        echo "🌐 配置OpenAI API..."
        export MODEL_BASE_URL="https://api.openai.com/v1"
        export MODEL_NAME="gpt-4-vision-preview"
        
        if [ -z "$OPENAI_API_KEY" ]; then
            echo "❌ 请设置OPENAI_API_KEY环境变量"
            echo "   export OPENAI_API_KEY=sk-your-api-key-here"
            exit 1
        fi
        export MODEL_API_KEY="$OPENAI_API_KEY"
        ;;
        
    azure)
        echo "☁️  配置Azure OpenAI..."
        
        if [ -z "$AZURE_OPENAI_ENDPOINT" ] || [ -z "$AZURE_OPENAI_API_KEY" ]; then
            echo "❌ 请设置Azure OpenAI环境变量:"
            echo "   export AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/"
            echo "   export AZURE_OPENAI_API_KEY=your-api-key"
            exit 1
        fi
        
        export MODEL_BASE_URL="$AZURE_OPENAI_ENDPOINT"
        export MODEL_API_KEY="$AZURE_OPENAI_API_KEY"
        export MODEL_NAME="gpt-4-vision"
        ;;
        
    alibaba)
        echo "🇨🇳 配置阿里云通义千问..."
        export MODEL_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
        export MODEL_NAME="qwen-vl-plus"
        
        if [ -z "$DASHSCOPE_API_KEY" ]; then
            echo "❌ 请设置DASHSCOPE_API_KEY环境变量"
            echo "   export DASHSCOPE_API_KEY=sk-your-api-key-here"
            exit 1
        fi
        export MODEL_API_KEY="$DASHSCOPE_API_KEY"
        ;;
esac

echo ""
echo "📋 当前配置:"
echo "   服务类型: $MODEL_CLIENT_TYPE"
echo "   服务地址: $MODEL_BASE_URL"
echo "   模型名称: $MODEL_NAME"
echo ""

# 启动Web应用
echo "🌐 启动Web应用..."
echo "📱 应用将在 http://localhost:8000 启动"
echo "🛑 按 Ctrl+C 停止服务"
echo ""

# 启动FastAPI应用
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
